<template>
  <div>
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="标签名称：">
        <el-input v-model.trim="query.name"></el-input>
      </el-form-item>
      <el-form-item label="分类名称：">
        <el-select v-model="query.categoryId" clearable filterable>
          <el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        <el-button icon="el-icon-circle-plus-outline" type="primary" @click="openAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <edit :categoryList="categoryList" :title="edit.title" :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose"></edit>

    <template>
      <div class="app-container">
        <!-- 数据列表 :data 绑定渲染的数据, border 纵向边框 -->
        <el-table :data="list" border highlight-current-row style="width: 100%">
          <!-- type="index"获取索引值，从1开始 ，label显示标题，prop 数据字段名，width列宽 -->
          <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
          <el-table-column align="center" prop="name" label="标签名称"></el-table-column>
          <el-table-column align="center" prop="categoryName" label="分类名称"></el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </template>
    <template>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.current"
          :page-sizes="[10, 20, 30]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
        ></el-pagination>
      </div>
    </template>
  </div>
</template>

<script>
import api from '@/api/label';
import categoryApi from '@/api/category';
import Edit from './edit';
export default {
  components: { Edit },
  name: 'Label',
  data() {
    return {
      list: [],
      page: {
        total: 0,
        current: 1,
        size: 20
      },
      query: {},
      categoryList: [], //存储正常状态的分类数组
      edit: {
        title: '',
        visible: false,
        formData: {}
      }
    };
  },
  created() {
    this.fetchData();
    this.getCategoryList();
  },
  methods: {
    openAdd() {
      this.getCategoryList();
      this.edit.visible = true;
      this.edit.title = '新增';
    },
    remoteClose() {
      this.edit.formData = {};
      this.edit.visible = false;
      this.fetchData();
    },
    reload() {
      this.query = {};
      this.fetchData();
    },
    queryData() {
      this.page.current = 1;
      this.fetchData();
    },
    getCategoryList() {
      categoryApi.getNormalList().then((response) => {
        this.categoryList = response.data;
      });
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.fetchData();
    },
    handleEdit(id) {
     // console.log('编辑', id);
     api.getById(id).then(response=>{
       if(response.code === 20000){
         this.edit.formData = response.data
         this.edit.visible = true
         this.edit.title = '编辑'
       }
     })
    },
    handleDelete(id) {
      //console.log('删除', id);
     this.$confirm('确认删除这条记录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
      }).then(() => {
      // 确认
      api.deleteById(id).then(response => {
      //提示信息
      this.$message({
      type: response.code===20000 ? 'success':'error',
      message: response.message
      })
      // 刷新列表
      this.fetchData()
      })
      }).catch(() => {
      // 取消删除，不理会
      })
    },
    fetchData() {
      api.getList(this.query, this.page.current, this.page.size).then((response) => {
        this.list = response.data.records;
        this.page.total = response.data.total;
      });
    }
  }
};
</script>

<style></style>
